# prerequisite: GOROOT and GOARCH must be defined

# defines $(GC) (compiler), $(LD) (linker) and $(O) (architecture)
include $(GOROOT)/src/Make.$(GOARCH)

# name of the package (library) being built
TARG=gomake

# source files in package
GOFILES=\
	src/gomake/gomake.go \
	src/gomake/gofiles.go \
	src/gomake/gopkgs.go \
	src/gomake/build.go \
	src/gomake/specialdeps.go \

DEPGRAPHFILES=\
	src/gomake/depgraph/depgraph.go \

# build "main" executable
.PHONY: main
main: package
	$(GC) -I_obj src/gm.go
	$(LD) -L_obj -o $(TARG) gm.$O
	@echo "Done. Executable is: $(TARG)"

clean:
	rm -rf _obj gm.$O

.PHONY: package
package: _obj/gomake.a _obj/gomake/depgraph.a


# create a Go package file (.a)
_obj/gomake.a: _obj/_gomake_.$O
	@mkdir -p _obj/
	rm -f _obj/$(TARG).a
	gopack grc $@ $<

_obj/gomake/depgraph.a: _obj/_depgraph_.$O
	@mkdir -p _obj/gomake/
	rm -f _obj/$(TARG).a
	gopack grc $@ $<

# compile
_obj/_gomake_.$O: $(GOFILES) _obj/gomake/depgraph.a
	@mkdir -p _obj/
	$(GC) -I_obj/ -o $@ $(GOFILES)

_obj/_depgraph_.$O: $(DEPGRAPHFILES)
	@mkdir -p _obj/
	$(GC) -o $@ $(DEPGRAPHFILES)

